#!/user/bin/env python
# -*- coding:utf-8 -*-
# author : 洛月
# mailer  : 2870093961@qq.com
# created: 2023-03-21 21:55
# updated: 2023-03-21 21:55

import json
import math
from datetime import datetime
from typing import Any
from starlette.responses import JSONResponse


class CustomJSONEncoder(json.JSONEncoder):
    def default(self, field):
        print(field)

        if isinstance(field, datetime):
            return math.floor(field.timestamp())
        if isinstance(field, dict):
            print({k: v for k, v in field.items() if v is not None})
            return {k: v for k, v in field.items() if v is not None}
        elif isinstance(field, list):
            print(123123)
            return [self.default(i) for i in field if i is not None]
        return super().default(field)


class CustomJSONResponse(JSONResponse):
    def render(self, content: Any) -> bytes:
        return json.dumps(
            content,
            ensure_ascii=False,
            allow_nan=False,
            indent=None,
            separators=(",", ":"),
            cls=CustomJSONEncoder
        ).encode("utf-8")
